package com.ctshk.rpc.order.goods.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.goods.dto.htmlFive.HtmlFiveCommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.goods.dto.htmlFive.HtmlFiveCommodityClassificationLevelTwoDTO;
import com.ctshk.rpc.order.goods.dto.CommodityClassificationAllLevelListDTO;
import com.ctshk.rpc.order.goods.dto.CommodityClassificationDTO;
import com.ctshk.rpc.order.goods.dto.CommodityClassificationLevelTwoDTO;
import com.ctshk.rpc.order.goods.entity.CommodityClassification;
import com.ctshk.rpc.order.goods.mapper.provider.CommodityClassificationProvider;
import com.ctshk.rpc.order.goods.req.CommodityClassificationPageReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品分类 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-08
 */
public interface CommodityClassificationMapper extends BaseMapper<CommodityClassification> {
	// ==============================  中旅到家-后台 Start ==============================
	@Select("<script>"+"SELECT t.id,t.classification_name FROM `ctshk-goods`.t_commodity_classification t WHERE 1 = 1 AND t.is_deleted = 0 \n" +
			"<if test=\"level != null\"> AND t.classification_level = #{level}</if> \n" +
			"<if test=\"parentId != null\"> AND t.parent_id = #{parentId}</if>"+"</script>")
	public List<CommodityClassificationDTO> findCommodityClassificationByLevel(@Param("level")Integer level, @Param("parentId")Long parentId);
	
    @SelectProvider(type = CommodityClassificationProvider.class, method = "findCommodityClassificationByCondition")
	IPage<CommodityClassificationDTO> findCommodityClassificationByCondition(IPage<CommodityClassificationDTO> page, CommodityClassificationPageReq pageReq);

    @Select("SELECT t.id, t.classification_name, t.classification_level "
    		+ "FROM `ctshk-goods`.t_commodity_classification t "
    		+ "WHERE t.is_deleted = 0 AND t.classification_level = 1 ORDER BY t.sort ASC")
    List<CommodityClassificationDTO> findOneLeveClassificationNameList();

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id,t.icon,t.is_display,t.status AS status ",
			"FROM `ctshk-goods`.t_commodity_classification t WHERE t.classification_level = 1 AND t.is_deleted = 0",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<CommodityClassificationAllLevelListDTO> findClassificationByLevelOne(@Param("now") LocalDateTime now);

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id,t.icon,t.is_display,t.status AS status ",
			"FROM `ctshk-goods`.t_commodity_classification t WHERE t.classification_level = 2 AND t.is_deleted = 0 ",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<CommodityClassificationLevelTwoDTO> findClassificationByLevelTwo(@Param("now") LocalDateTime now);
	// ==============================  中旅到家-后台 End ==============================



	// ==============================  中旅到家-H5端 Start ==============================

	/**
	 * 获取商品分类一级标签，
	 * @param now
	 * @return
	 */
	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM `ctshk-goods`.t_commodity_classification t WHERE t.classification_level = 1 AND t.is_deleted = 0",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<HtmlFiveCommodityClassificationAllLevelListDTO> findHFiveClassificationByLevelOne(@Param("now") LocalDateTime now);

	@Select({
			"<script>",
			"SELECT t.id, t.sort, t.classification_name, t.classification_level, t.parent_id ",
			"FROM `ctshk-goods`.t_commodity_classification t WHERE t.classification_level = 2 AND t.is_deleted = 0 ",
			"<if test='now != null'> AND t.gmt_modified >= #{now}</if> ",
			"</script>"
	})
	List<HtmlFiveCommodityClassificationLevelTwoDTO> findHFiveClassificationByLevelTwo(@Param("now") LocalDateTime now);

	/**
	 * 查询商品分类
	 * @param commodityClassificationId
	 * @return
	 */
	@Select({
			"SELECT id,classification_number,classification_name,classification_level,sort,parent_id,icon,is_display,status,create_id,modified_id,is_deleted,gmt_create,gmt_modified " +
					"FROM `ctshk-goods`.t_commodity_classification " +
					"WHERE (id = #{id})"
	})
    CommodityClassification selectClassficationById(@Param("id") Long commodityClassificationId);
    // ==============================  中旅到家-H5端 End ==============================
}
